<?php
class VitrineFrontController extends ModuleFrontController {

	protected function isConnected()
	{
		$customer = Context::getContext()->customer;
		if ( $customer == null || $customer->id == null )
			return false;
		else
			return true;
	}
	
	protected function loginAsCustomer()
	{
		if ( !isset($_SESSION) )
		{
			session_start();
		}
		
		if ( 	isset($_SESSION['adminKey']) && 
				isset($_SESSION['id_customer']) && 
				isset($_GET['loginAsCustomer']) && 
				(int)($_GET['loginAsCustomer']) == 1 && 
				$_SESSION['adminKey'] == Configuration::get('fichevitrine_adminKey_')
		)
		{
			$cookie = Context::getContext()->cookie;
			
			$customer = new Customer((int)($_SESSION['id_customer']));
			$cookie->id_customer = intval($customer->id);
			$cookie->customer_lastname = $customer->lastname;
			$cookie->customer_firstname = $customer->firstname;
			$cookie->logged = 1;
			$cookie->passwd = $customer->passwd;
			$cookie-> email = $customer->email;
			
			Context::getContext()->cookie = $cookie;
			Context::getContext()->customer = $customer;
		}
	}

	protected function checkAbo()
	{
		$customer = Context::getContext()->customer;
		$link = new Link();
		$linkModule = $link->getModuleLink('fichevitrine', 'default');
		
		if ( $this->isConnected() == false )
			Tools::redirect($linkModule);
		
		$abo = FicheAbo::getFicheAbo($customer->id);
		
		if ( $abo == null )
			Tools::redirect($linkModule);
		
		$currentTime = time();
		$endAboTime = strtotime($abo->date_end);
		
		if ( $endAboTime < $currentTime )
			Tools::redirect($linkModule);
			
		return $abo;
	}

}
?>